package com.hbnu.service.impl;

import com.hbnu.mapper.CartMapper;
import com.hbnu.pojo.Collection;
import com.hbnu.pojo.Good;
import com.hbnu.pojo.Request;
import com.hbnu.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    @Override
    public void addToCart(Request rq) {
        cartMapper.insertCart(rq.getUid(), rq.getGid(), rq.getCount());
    }

    @Override
    public void delFromCart(Request rq) {
        cartMapper.deleteCart(rq.getUid(), rq.getGid());
    }

    @Override
    public List<Collection> getCollectionList(Request rq) {
        List<Collection> collectionList=cartMapper.getCartListByUid(rq.getUid());
        collectionList.stream().forEach(collection -> {
            Good good=cartMapper.getGoodByGid(collection.getGid());
            BigDecimal tolPrice=new BigDecimal(good.getPrice());
            tolPrice=tolPrice.multiply(new BigDecimal(Integer.toString(collection.getCount())));
            collection.setTolPrice(tolPrice);
            collection.setGood(good);
        });
        return collectionList;
    }

    @Override
    public void modifyCollection(Request rq) {
        cartMapper.updateCart(rq.getCount(),rq.getGid(),rq.getUid());
    }
}
